import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

// 接口封装
import minApi from '@/api/api'

const store = new Vuex.Store({
	modules: {},
	state: {
		departs: null,
		userInfo: null,
		otherUserInfo: {}, //分享来的他人信息
		openId: null,
		isSelf: true, //是否是本人
		isEditNickName: false, // 判断用户是否第一次进入密聊，如果是提示修改名字保存状态，如果状态为false代表没修改密聊名字，true代表已修改密聊名字
		visitNumber: 0,
		companyStatus: 0, // 企业管理tab切换状态保存
		buyInfo: {}, //采购需求信息
		supplyInfo: {}, //供应信息
		productInfo: {}, //供应产品信息
		supplyProductArr: [], //产品供应信息数组
		myBuyInfo: {}, //我的采购历史信息
		mySupInfo: {}, //我的供应商历史信息
		chatListActivityIndex: 1, //聊天tabs 状态保存
		// newPartyList: [], //新组局邀请的人员
		// newPartyInfo: {}, //创建组局时的选择信息
		// partyMemberListInStore: [], //新开组局参加人员列表
		// maxCount: 6,
		isCheckingType: false, // 小程序审核中
		haveSupplier: false, //有无完成供应商信息
		supplierId: false, //供应商ID
		supplierInfo: '', //供应商信息
		wxCheckType: null, //微信审核状态
	},
	mutations: {
		setCompanyStatus(state, data) {
			state.companyStatus = data
			uni.setStorageSync(`companyStatus`, data)
		},
		setVisitNumber(state, data) {
			state.visitNumber = data
			uni.setStorageSync(`visitNumber`, data)
		},
		userInfo(state, data) {
			state.userInfo = data
			uni.setStorageSync(`_userInfo`, data)
		},
		// 快速建群
		quickJoinGroupUserInfo(state, data) {
			uni.setStorageSync(`_quickJoinGroupUserInfo`, data)
		},
		setOtherUserInfo(state, data) {
			state.otherUserInfo = data
			uni.setStorageSync(`_otherUserInfo`, data)
		},
		setOpenId(state, data) {
			Vue.set(state, 'openId', data)
			uni.setStorageSync(`openId`, data)
		},
		setIsSelf(state, data) {
			state.isSelf = data
		},
		setEditNickName(state, data) {
			state.isEditNickName = data
		},
		setBuyInfo(state, data) {
			state.buyInfo = data
		},
		setSupplyInfo(state, data) {
			state.supplyInfo = data
		},
		setProductInfo(state, data) {
			state.productInfo = data
		},
		setSupplyProductArr(state, data) {
			console.log()
			state.supplyProductArr.push(data)
			uni.setStorageSync(`supplyProductArr`, state.supplyProductArr)
		},
		setMyBuyInfo(state, data) {
			console.log()
			state.myBuyInfo = data
		},
		setMySupInfo(state, data) {
			console.log()
			state.mySupInfo = data
		},
		//聊天tabs 状态保存
		setChatListActivityIndex(state, data) {
			state.chatListActivityIndex = data
		},
		isChecking(state, data) {
			state.isCheckingType = data
		},
		setSupplierId(state, data) {

			uni.setStorageSync(`supplierId`, data)
		},
		setSupplierInfo(state, data) {
			state.supplierInfo = data
			state.supplierId = data ? data.id : null
			state.haveSupplier = !!state.supplierId
			console.log(data, 'setSupplierInfo')
			uni.setStorageSync(`supplierInfo`, data)
			uni.setStorageSync(`supplierId`, state.supplierId)
		},
		changeWxcheckType(state, data) {
			state.wxCheckType = data
		}

	},
	getters: {
		token(state) {
			if (state.token) {
				return state.token
			}
			return uni.getStorageSync(`_token`)
		},
		openId(state) {
			if (state.userInfo && state.userInfo.openId) {
				return state.userInfo.openId
			}
			let tUserInfo = uni.getStorageSync(`_userInfo`)
			if (tUserInfo && tUserInfo.openId) {
				return tUserInfo.openId
			}
			if (state.openId) {
				return state.openId.openid
			}
			return (uni.getStorageSync(`openId`) || {}).openid || ''
		},
		userInfo(state) {
			if (state.userInfo) {
				return state.userInfo
			}
			return uni.getStorageSync(`_userInfo`)
		},
		quickGroupUserInfo(state) {
			if (state.userInfo && state.userInfo.id) {
				return state.userInfo
			}
			let tUserInfo = uni.getStorageSync(`_userInfo`)
			if (tUserInfo && tUserInfo.id) {
				return tUserInfo
			}
			return uni.getStorageSync(`_quickJoinGroupUserInfo`) || {}
		},
		otherUserInfo(state) {
			if (state.otherUserInfo) {
				return state.otherUserInfo
			}
			return uni.getStorageSync(`_otherUserInfo`)
		},
		supplyProductArr(state) {
			if (state.supplyProductArr) {
				return state.supplyProductArr
			}
			return uni.getStorageSync(`supplyProductArr`)
		},
		getSupplierId(state) {
			if (state.supplierId) {
				return state.supplierId
			}
			return uni.getStorageSync(`supplierId`)
		},
		getSupplierInfo(state, data) {
			if (state.supplierInfo) {
				return state.supplierInfo
			}
			return uni.getStorageSync(`supplierInfo`)
		},
		getHaveSupplier(state) {
			if (state.supplierId) {
				return true
			}
			return !!uni.getStorageSync(`supplierId`)
		},
	},
	actions: {
		//使用code 获取openId
		async setOpenId(context) {
			uni.login({
				provider: 'weixin',
				onlyAuthorize: true,
				success: async (loginRes) => {
					let res = await minApi.getOpenIdByCode({
						code: loginRes.code
					});
					uni.setStorageSync({
						data: res,
						key: 'openId',
						success: function(res) {
							console.log('存储openid信息成功');
						}
					})
				}
			});
		},
		//获取openId
		async getOpenId({
			state,
			dispatch,
			commit
		}) {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'weixin',
					onlyAuthorize: true,
					success: async (loginRes) => {
						let code = loginRes.code
						let payload = {
							code
						}
						let res = await minApi.getOpenIdByCode(payload);
						if (res) {
							commit('setOpenId', res)
							resolve(res)
						} else {
							uni.showToast({
								title: '获取openID失败',
								icon: 'none'
							});
						}
					}
				});
			});
		},
		// 查询待参加组局的数量徽标
		async queryJoinPartyNumber(context, params) {
			console.log(params, '查询待参加组局的数量徽标');
			try {
				let res = await minApi.queryJoinPartyNumber(params);
				if (res && res.code === 200) {
					return {
						success: true,
						data: res.result
					}
				} else {
					return {
						success: false,
						msg: res.msg || '系统异常,请稍后再试!'
					}
				}
			} catch (err) {
				return {
					success: false,
					msg: '系统异常,请稍后再试!'
				}
			}
		},
		//根据id 待发布组局中的list item
		async delSendPartyItemById(context) {
			try {
				let res = await minApi.delSendPartyItemById();
				if (res && res.code === 200) {
					return {
						success: true,
						data: res.result
					}
				} else {
					return {
						success: false,
						msg: res.msg || '系统异常,请稍后再试!'
					}
				}
			} catch (err) {
				return {
					success: false,
					msg: '系统异常,请稍后再试!'
				}
			}
		},
		//修改用户收藏状态
		async changIsFavorite(context, params) {
			try {
				let payload = {
					openId: params.userInfo.openId,
					isFavorite: params.isFavorite
				}
				let res = await minApi.changIsFavorite(payload);
				if (res && res.code === 200) {
					return {
						success: true,
						data: res.result
					}
				} else {
					return {
						success: false,
						msg: res.msg || '系统异常,请稍后再试!'
					}
				}
			} catch (err) {
				return {
					success: false,
					msg: '系统异常,请稍后再试!'
				}
			}
		},
		//查询审核状态
		async wxcheckType({
			commit
		}) {
			try {
				let res = await minApi.queryAppAudit();
				if (res.code === 200) {
					console.log('changeWxcheckType-success');
					commit('changeWxcheckType', res.result)
				}
			} catch (err) {
				console.log('changeWxcheckType-error');
				commit('changeWxcheckType', false)
			}
		},
	}
})
export default store